﻿using System;
using System.Windows.Forms;
using HelperLib;
using InputControls;
using WiimoteLib;
using System.Drawing;

namespace WiimoteTest
{
   public partial class Test1 : Form
   {
      private readonly Wiimote wm = new Wiimote();
      private WiimoteEventWrapper wmwrapper;
      private bool bDown;
      private bool bLeft;
      private bool[][] blink;
      private int blinkIdx;
      private bool bRight;
      private bool bUp;
      private WiimoteChangedEventArgs changedEvents;
      private WiimoteExtensionChangedEventArgs ExtensionChangedEvent;

      public Test1()
      {
         
         KukaCoordData kcd = new KukaCoordData()
                                  {
                                     Coordinates = new KukaCoordinates()
                                                      {
                                                         A1 = 1,
                                                         A2 = 2,
                                                         A3 = 3
                                                      },
                                     CoordType = KukaCoordType.AxisCoords

                                  };
         
         InitializeComponent();
         blink = new[]
                    {
                       new[] {true, false, false, false},
                       new[] {false, false, true, false},
                       new[] {false, true, false, false},
                       new[] {false, false, false, true},
                    };
         selectControl = new MoveControl[]
                            {
                               moveControl1,
                               moveControl2,
                               moveControl3,
                               moveControl4,
                               moveControl5,
                               moveControl6
                            };
         SelectControl(0);
      }

      private MoveControl[] selectControl;
      private int selectedControlIdx = 0;

      private void SelectControl(int offset)
      {

         selectControl[selectedControlIdx].BackColor = System.Drawing.SystemColors.Control;
         selectedControlIdx = (selectedControlIdx + offset + selectControl.Length) %
                              selectControl.Length;
         selectControl[selectedControlIdx].BackColor = Color.OrangeRed;
      }
      private void AddToSelectedControl(int value)
      {
         MoveControl cControl = selectControl[selectedControlIdx];
         float cVal = (float) value/100f;

         cControl.Speed += cVal;
      }
      private void ToggleControlActive()
      {
         selectControl[selectedControlIdx].Active = !selectControl[selectedControlIdx].Active;
      }
      private void wmwrapper_OnButtonCChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker)delegate { label15.Text = "C: " + (e.Pressed ? "Pressed" : "Released"); });
      }

      private void wmwrapper_OnButtonZChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker)delegate { label16.Text = "Z: " + (e.Pressed ? "Pressed" : "Released"); });
      }



      private void wmwrapper_OnButtonHomeChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate { label12.Text = "Home: " + (e.Pressed ? "Pressed" : "Released"); });
      }

      private void wmwrapper_OnButtonPlusChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate { label13.Text = "Plus: " + (e.Pressed ? "Pressed" : "Released"); });
      }

      private void wmwrapper_OnButtonMinusChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate { label14.Text = "Minus: " + (e.Pressed ? "Pressed" : "Released"); });
      }

      private void wmwrapper_OnButtonLeftChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate
                                   {
                                      label11.Text = (e.Pressed ? "x" : "o");

                                      if (e.Pressed)
                                         SelectControl(-1);
  });
      }

      private void wmwrapper_OnButtonDownChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate
                                   {
                                      label10.Text = (e.Pressed ? "x" : "o");

                                      if (e.Pressed)
                                         AddToSelectedControl(-10);
                                   });
      }

      private void wmwrapper_OnButtonRightChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate
                                   {
                                      label9.Text = (e.Pressed ? "x" : "o");

                                      if (e.Pressed)
                                         SelectControl(1);

                                   });
      }

      private void wmwrapper_OnButtonUpChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate
                                   {
                                      label8.Text = (e.Pressed ? "x" : "o");

                                      if (e.Pressed)
                                         AddToSelectedControl(10);
                                   });
      }

      private void wmwrapper_OnButton1Changed(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate { label7.Text = "Button 1: " + (e.Pressed ? "Pressed" : "Released"); });
      }

      private void wmwrapper_OnButton2Changed(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate { label6.Text = "Button 2: " + (e.Pressed ? "Pressed" : "Released"); });
      }

      private void wmwrapper_OnBButtonChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate {
            label5.Text = "Button B: " + (e.Pressed ? "Pressed" : "Released"); 
            wm.SetRumble(e.Pressed);
         });
      }

      private void wmwrapper_OnAButtonChanged(object sender, ButtonStateChangedEventArgs e)
      {
         Invoke((MethodInvoker) delegate
                                   {
                                      label4.Text = "Button A: " + (e.Pressed ? "Pressed" : "Released");
                                      if (e.Pressed)
                                         ToggleControlActive();
                                   });
      }

      private void blinkTimer_Tick(object sender, EventArgs e)
      {
         //label1.Text = "X: " + meanX.Mean();
         //label2.Text = "Y: " + meanY.Mean();
         //label3.Text = "Z: " + meanZ.Mean();

         //if (changedEvents.WiimoteState.ButtonState.B)
         //{
         //   wm.SetLEDs(blink[blinkIdx][0], blink[blinkIdx][1], blink[blinkIdx][2], blink[blinkIdx][3]);
         //   blinkIdx = ++blinkIdx % blink.Length;
         //}
         //if (changedEvents.WiimoteState.ButtonState.A)
         //{
         //   wm.SetRumble(true);
         //}
         //else
         //{
         //   wm.SetRumble(false);
         //}
      }

      private void Test1_Load(object sender, EventArgs e)
      {
         wm.WiimoteChanged += wm_WiimoteChanged;
         wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
         wm.Connect();

         wm.SetReportType(InputReport.IRAccel, true);

         wmwrapper = new WiimoteEventWrapper(wm);
         wmwrapper.OnButtonAChanged += wmwrapper_OnAButtonChanged;
         wmwrapper.OnButtonBChanged += wmwrapper_OnBButtonChanged;
         wmwrapper.OnButton1Changed += wmwrapper_OnButton1Changed;
         wmwrapper.OnButton2Changed += wmwrapper_OnButton2Changed;
         wmwrapper.OnButtonUpChanged += wmwrapper_OnButtonUpChanged;
         wmwrapper.OnButtonRightChanged += wmwrapper_OnButtonRightChanged;
         wmwrapper.OnButtonDownChanged += wmwrapper_OnButtonDownChanged;
         wmwrapper.OnButtonLeftChanged += wmwrapper_OnButtonLeftChanged;

         wmwrapper.OnButtonMinusChanged += wmwrapper_OnButtonMinusChanged;
         wmwrapper.OnButtonPlusChanged += wmwrapper_OnButtonPlusChanged;
         wmwrapper.OnButtonHomeChanged += wmwrapper_OnButtonHomeChanged;

         wmwrapper.OnButtonCChanged += wmwrapper_OnButtonCChanged;
         wmwrapper.OnButtonZChanged += wmwrapper_OnButtonZChanged;

         wmwrapper.OnWiiAccelUpdate += wmwrapper_OnWiiAccelUpdate;
         wmwrapper.OnNunchukAccelUpdate += wmwrapper_OnNunchukAccelUpdate;
         wmwrapper.OnNunchukJoystickUpdate += wmwrapper_OnNunchukJoystickUpdate;
         wmwrapper.MeanInterval = 250;
      }

      void wmwrapper_OnNunchukJoystickUpdate(object sender, Point2FUpdateEventArgs e)
      {
         label22.Text = "JoyStick X: " + e.X;
         label21.Text = "JoyStick Y: " + e.Y;
      }

      void wmwrapper_OnNunchukAccelUpdate(object sender, Point3FUpdateEventArgs e)
      {
         label19.Text = "Nunchuk X: " + e.Coordinates.X;
         label18.Text = "Nunchuk Y: " + e.Coordinates.Y;
         label17.Text = "Nunchuk Z: " + e.Coordinates.Z;
      }

      void wmwrapper_OnWiiAccelUpdate(object sender, Point3FUpdateEventArgs e)
      {
         label1.Text = "Wiimote X: " + e.Coordinates.X;
         label2.Text = "Wiimote Y: " + e.Coordinates.Y;
         label3.Text = "Wiimote Z: " + e.Coordinates.Z;
      }

      private void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs e)
      {
         ExtensionChangedEvent = e;
         
      
      }

      private void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs e)
      {
         //wmwrapper.ParseWiimoteChangedEvent(e);
         changedEvents = e;
         //meanX.Add(e.WiimoteState.AccelState.Values.X);
         //meanY.Add(e.WiimoteState.AccelState.Values.Y);
         //meanZ.Add(e.WiimoteState.AccelState.Values.Z);
      }
   }
}